package com.wewins.fota.mapper;

import com.wewins.fota.bean.bo.filter.FilterCond;
import com.wewins.fota.bean.po.PkgVersion;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PkgVersionMapper {

    @Select({
            "<script>",
            "SELECT * FROM pkg_version WHERE 1=1 ",
            "<if test=\"product != null and product != ''\" >",
            "AND product=#{product}",
            "</if>",
            "<if test=\"version != null and version != ''\" >",
            "AND version=#{version}",
            "</if>",
            "<if test=\"description != null and description != ''\" >",
            "AND locate(#{description}, description) > 0",
            "</if>",
            "<if test='minTime != null and minTime != 0' >",
            "<![CDATA[ AND create_time>=#{minTime} ]]>",
            "</if>",
            "<if test='maxTime != null and maxTime != 0' >",
            "<![CDATA[ AND create_time<=#{maxTime} ]]>",
            "</if>",
            "</script>"
    })
    @Results(id = "pkgVersion_query", value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "product", column = "product"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time"),
            @Result(property = "releaseTime", column = "release_time"),
            @Result(property = "sizeText", column = "size_text"),
    })
    List<PkgVersion> queryAll(FilterCond cond);

    @Select("SELECT * FROM pkg_version WHERE id = #{id}")
    @ResultMap(value = "pkgVersion_query")
    PkgVersion findById(int id);

    @Select("SELECT * FROM pkg_version WHERE product = #{product} AND version = #{version}")
    @ResultMap("pkgVersion_query")
    PkgVersion findByProductVersion(String product, String version);

    @Select({"<script>",
            "SELECT * FROM pkg_version WHERE product = #{product} AND version IN",
            "<foreach collection = 'versions' item = 'item' index = 'index' separator = ',' open='(' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"})
    @ResultMap("pkgVersion_query")
    List<PkgVersion> findAllByProductVersions(String product, List<String> versions);

    @Select("SELECT distinct product FROM pkg_version")
    List<String> findAllProducts();

    @Select("SELECT distinct version FROM pkg_version WHERE product = #{product}")
    List<String> findVersionsByProduct(String product);

    @Update("UPDATE pkg_version SET product = #{product}, version = #{version}, flag = #{flag}, uri = #{uri}, create_time = #{createTime}, " +
            "update_time = #{updateTime}, release_time = #{releaseTime}, size = #{size}, size_text = #{sizeText}, description = #{description}, remark = #{remark} WHERE id = #{id}")
    int update(PkgVersion pkgVersion);

    int updateByBatch(List<PkgVersion> pkgVersionList);

    @Insert("INSERT INTO pkg_version (product, version, flag, uri, create_time, update_time, release_time, size, size_text, description, remark) VALUES " +
            "(#{product}, #{version}, #{flag}, #{uri}, #{createTime}, #{updateTime},  #{releaseTime},  #{size},  #{sizeText}, #{description}, #{remark})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(PkgVersion pkgVersion);

    @Insert({
            "<script>",
            "INSERT INTO pkg_version (product, version, flag, uri, create_time, update_time, release_time, size, size_text, description, remark) VALUES ",
            "<foreach collection = 'srcPkgVersions' item = 'item' index = 'index' separator = ','>",
            "(#{item.product}, #{item.version}, #{item.flag}, #{item.uri}, #{item.createTime}, #{item.updateTime}, #{item.releaseTime}, #{item.size}, #{item.sizeText}, #{item.description}, #{item.remark})",
            "</foreach></script>"
    })
    int insertByBatch(List<PkgVersion> pkgVersionList);

    @Delete("DELETE FROM pkg_version WHERE id = #{id}")
    int delete(PkgVersion pkgVersion);
}

